package com.aijk.web.admin.controller.info;

import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.*;
import com.aijk.ehm.model.*;
import com.aijk.ehm.model.qo.*;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.model.MhInfoExt;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 资讯
 */
@Controller
@RequestMapping(value = "/admin/info")
public class InfoController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(InfoController.class);

    @Autowired
    private EhmInfoApi mhInfoApi;

    @Autowired
    private EhmInfoSrcApi mhInfoSrcApi;

    @Autowired
    private EhmInfoClassifyApi mhInfoClassifyApi;

    @Autowired
    private EhmInfoEvaluateApi mhInfoEvaluateApi;

    @Autowired
    private EhmInfoScanApi mhInfoScanApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        EhmInfoSrcQo mhInfoSrcQo = new EhmInfoSrcQo();
        //mhInfoSrcQo.setStatus((short) 1);
        List<EhmInfoSrc> list = mhInfoSrcApi.list(mhInfoSrcQo);
        List<EhmInfoClassify> list1 = mhInfoClassifyApi.list(new EhmInfoClassifyQo());
        model.addAttribute("cList", list1);
        model.addAttribute("sList", list);
        return "info/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Long draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        draw += 1;
        EhmInfoQo qo = DataTableUtil.fillQo(skey, EhmInfoQo.class);

        PageInfo<EhmInfo> pageInfo = mhInfoApi.page(qo);

        List<MhInfoExt> list = new ArrayList<>();
        List<EhmInfo> members = pageInfo.getList();
        if (members != null && members.size() > 0) {
            list = PojoUtil.convert(members, MhInfoExt.class);
            for (MhInfoExt mhMemberExt : list) {
                if (mhMemberExt.getClassifyId() != null){
                    EhmInfoClassify mhInfoClassify = mhInfoClassifyApi.findOne(mhMemberExt.getClassifyId());
                    if (mhInfoClassify != null){
                        mhMemberExt.setClassify(mhInfoClassify.getTitle());
                    }
                }
                if (mhMemberExt.getSrcId() != null){
                    EhmInfoSrc mhInfoSrc = mhInfoSrcApi.findOne(mhMemberExt.getSrcId());
                    if (mhInfoSrc != null){
                        mhMemberExt.setSrc(mhInfoSrc.getTitle());
                    }
                }
            }
        }


        iTotalRecords = pageInfo.getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("data", list);
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        EhmInfo bean = new EhmInfo();
        if (id != null){
            	bean = mhInfoApi.findOne(id);
        }

        EhmInfoSrcQo peCenterSectionQo = new EhmInfoSrcQo();
        peCenterSectionQo.setStatus((short)1);
        List<EhmInfoSrc> peCenterSectionVoList = mhInfoSrcApi.list(peCenterSectionQo);

        EhmInfoClassifyQo peCenterClassifyQo = new EhmInfoClassifyQo();
        peCenterClassifyQo.setStatus((short)1);
        List<EhmInfoClassify> peCenterClassifyVoList = mhInfoClassifyApi.list(peCenterClassifyQo);

        /*EhmInfoClassify classify = mhInfoClassifyApi.findOne(bean.getClassifyId());
        if (classify != null) {
            if (classify.getpClassifyid() != 0) {
                model.addAttribute("sid", bean.getClassifyId());
                bean.setClassifyId(classify.getpClassifyid());
            }else {
                model.addAttribute("sid", -1);
            }
        }else{
            model.addAttribute("sid", -1);
        }*/

        model.addAttribute("cicn",peCenterClassifyVoList);
        model.addAttribute("src",peCenterSectionVoList);

        model.addAttribute("mhInfo", bean);
        return "info/edit";
    }


   /* @RequestMapping(value = "/second/classify", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg secondClassify(Long id) {
        if (id == null){
            return RetMsg.failed();
        }
        EhmInfoClassifyQo peCenterClassifyQo = new EhmInfoClassifyQo();
        peCenterClassifyQo.setStatus((short)1);
        //peCenterClassifyQo.setpClassifyid(id);
        List<EhmInfoClassify> secClassify = mhInfoClassifyApi.list(peCenterClassifyQo);
        if (secClassify.isEmpty()) {
            RetMsg retMsg = RetMsg.failed();
            EhmInfoClassify classify = mhInfoClassifyApi.findOne(id);
            if (classify != null) {
                //retMsg.put("type", classify.getType());
            }
            return retMsg;
        }
        RetMsg retMsg = RetMsg.success();
        retMsg.put("seclist", secClassify);
        //retMsg.put("type", secClassify.get(0).getType());

        return retMsg;
    }*/


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(EhmInfo bean) {
        try {
            	Long id = bean.getId();
            EhmInfo mhInfo = mhInfoApi.findOne(id);

            	if (id == null) {// 添加
                    bean.setIsSuggest((byte) 1);
                    bean.setStatus((short) 1);
                    bean.setCreateTime(new Date());
                    bean.setEvaluateNum(0);
                    bean.setScanNum(0);
                    bean.setShareNum(0);
                    bean.setIsHot((byte) 1);
                    if (!bean.getContent().isEmpty()){
                        String content = bean.getContent().replaceAll("static.aijk.com","115.236.19.153");
                        bean.setContent(content);
                    }
            		mhInfoApi.save(bean);
                    if (bean.getClassifyId() != null){
                        EhmInfoClassify mhInfoClassify = mhInfoClassifyApi.findOne(bean.getClassifyId());
                        Integer a = mhInfoClassify.getInfoNum() + 1;
                        mhInfoClassify.setInfoNum(a);
                        mhInfoClassifyApi.update(mhInfoClassify);
                    }
                    if (bean.getSrcId() != null){
                        EhmInfoSrc mhInfoSrc = mhInfoSrcApi.findOne(bean.getSrcId());
                        Integer b = mhInfoSrc.getInfoNum() + 1;
                        mhInfoSrc.setInfoNum(b);
                        mhInfoSrcApi.update(mhInfoSrc);
                    }
            	} else {
                    if (!bean.getContent().isEmpty()){
                        String content = bean.getContent().replaceAll("static.aijk.com","115.236.19.153");
                        bean.setContent(content);
                    }
                		mhInfoApi.update(bean);
                    if (!bean.getClassifyId().equals(mhInfo.getClassifyId())){
                        EhmInfoClassify mhInfoClassify = mhInfoClassifyApi.findOne(bean.getClassifyId());
                        Integer a = mhInfoClassify.getInfoNum() + 1;
                        mhInfoClassify.setInfoNum(a);
                        mhInfoClassifyApi.update(mhInfoClassify);

                        EhmInfoClassify mhInfoClassify2 = mhInfoClassifyApi.findOne(mhInfo.getClassifyId());
                        Integer b = mhInfoClassify2.getInfoNum() - 1;
                        mhInfoClassify2.setInfoNum(b);
                        mhInfoClassifyApi.update(mhInfoClassify2);
                    }
                    if (!bean.getSrcId().equals(mhInfo.getSrcId())){
                        EhmInfoSrc mhInfoSrc = mhInfoSrcApi.findOne(bean.getSrcId());
                        Integer a = mhInfoSrc.getInfoNum() + 1;
                        mhInfoSrc.setInfoNum(a);
                        mhInfoSrcApi.update(mhInfoSrc);

                        EhmInfoSrc mhInfoSrc2 = mhInfoSrcApi.findOne(mhInfo.getSrcId());
                        Integer b = mhInfoSrc2.getInfoNum() - 1;
                        mhInfoSrc2.setInfoNum(b);
                        mhInfoSrcApi.update(mhInfoSrc2);
                    }
                	}
            }catch (ErrorCodeException e){
                	e.printStackTrace();
                	logger.error(e.getMessage());
                	return RetMsg.failed();
            }
            return RetMsg.success();
        }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg deteleById(@RequestParam(value = "id") Long id) {
        try {
            EhmInfo mhInfo = mhInfoApi.findOne(id);
            mhInfoApi.deleteById(id);
            if (mhInfo.getSrcId() != null){
                EhmInfoSrc mhInfoSrc = mhInfoSrcApi.findOne(mhInfo.getSrcId());
                mhInfoSrc.setInfoNum(mhInfoSrc.getInfoNum() - 1);
                mhInfoSrcApi.update(mhInfoSrc);
            }
            if (mhInfo.getClassifyId() != null){
                EhmInfoClassify mhInfoClassify = mhInfoClassifyApi.findOne(mhInfo.getClassifyId());
                mhInfoClassify.setInfoNum(mhInfoClassify.getInfoNum() - 1);
                mhInfoClassifyApi.update(mhInfoClassify);
            }
        }catch (ErrorCodeException e){
            	e.printStackTrace();
            	logger.error(e.getMessage());
            	return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/grounding", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg grounding(@RequestParam(value = "id") Long id){
        EhmInfo basBanner = mhInfoApi.findOne(id);
        //SysAdmin admin = getCurrentUser();
        if(basBanner != null){
            short status = basBanner.getStatus();
            EhmInfo updateBanner = new EhmInfo();
            if(status == 1){
                status = 0;
                //updateBanner.setDeployer(admin.getName());
                updateBanner.setDeployTime(new Date());
            }else{
                status = 1;
            }
            updateBanner.setId(id);
            updateBanner.setStatus(status);
            mhInfoApi.update(updateBanner);
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/grounding1", method = RequestMethod.POST)
     @ResponseBody
     public RetMsg grounding1(@RequestParam(value = "id") Long id){
        EhmInfo basBanner = mhInfoApi.findOne(id);
        if(basBanner != null){
            byte status = basBanner.getIsSuggest();
            if(status == 1){
                status = 0;
            }else{
                status = 1;
            }
            EhmInfo updateBanner = new EhmInfo();
            updateBanner.setId(id);
            updateBanner.setIsSuggest(status);
            mhInfoApi.update(updateBanner);
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/grounding2", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg grounding2(@RequestParam(value = "id") Long id){
        EhmInfo basBanner = mhInfoApi.findOne(id);
        if(basBanner != null){
            byte status = basBanner.getIsHot();
            if(status == 1){
                status = 0;
            }else{
                status = 1;
            }
            EhmInfo updateBanner = new EhmInfo();
            updateBanner.setId(id);
            updateBanner.setIsHot(status);
            mhInfoApi.update(updateBanner);
        }
        return RetMsg.success();
    }



    @RequestMapping(value = "/eva/list", method = RequestMethod.GET)
    public String detail_list(@RequestParam(value = "id") Long id,Model model) {
        model.addAttribute("mid",id);
        return "info/evaluate/list";
    }

    @RequestMapping(value = "/eva/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg detail_list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        EhmInfoEvaluateQo qo = new EhmInfoEvaluateQo();
        qo.setNewsid(Utils.toLong(skey.get("mid")));
        PageInfo<EhmInfoEvaluate> mhMemberPageInfo = mhInfoEvaluateApi.page(qo);

        iTotalRecords =mhMemberPageInfo.getTotal();
        RetMsg regMsg = new RetMsg();

        regMsg.put("data", mhMemberPageInfo.getList());
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }


    @RequestMapping(value = "/scan/list", method = RequestMethod.GET)
    public String detail_list2(@RequestParam(value = "id") Long id,Model model) {
        model.addAttribute("mid",id);
        return "info/scan/list";
    }

    @RequestMapping(value = "/scan/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg detail_list2(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        EhmInfoScanQo qo = new EhmInfoScanQo();
        qo.setNewsid(Utils.toLong(skey.get("mid")));
        PageInfo<EhmInfoScan> mhMemberPageInfo = mhInfoScanApi.page(qo);

        iTotalRecords =mhMemberPageInfo.getTotal();
        RetMsg regMsg = new RetMsg();

        regMsg.put("data", mhMemberPageInfo.getList());
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }



}